#include "mainwindowimpl.h"
#include "myitemview.h"
#include "decoratorquestionnairemodel.h"
#include <QDebug>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QFileDialog>
//
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
	setupUi(this);
	iv = new MyItemView(this);
	QVBoxLayout * l = new QVBoxLayout(w);
	l->addWidget(iv);

	qDebug()<<1;
	m = new DecoratorQuestionnaireModel(this);
	qDebug()<<2;
	fileName = QFileDialog::getOpenFileName(this, trUtf8("Откройте файл анкеты."));
	m->open(fileName);
	qDebug()<<3;
	iv->setModel(m->model());
	qDebug()<<4;
	iv->setRootIndex(m->model()->index(0, 0));
	qDebug()<<5;
}
//

void MainWindowImpl::on_NextActn_triggered(){
	if(iv->rootIndex().row() < m->model()->rowCount()){
		iv->setRootIndex(m->model()->index(iv->rootIndex().row() + 1, 0));
		BackBtn->setEnabled(true);
		if(iv->rootIndex().row() == m->model()->rowCount() - 1)
			NextBtn->setEnabled(false);
	}
}

void MainWindowImpl::on_BackActn_triggered(){
	if(iv->rootIndex().row() >= 0){
		iv->setRootIndex(m->model()->index(iv->rootIndex().row() - 1, 0));
		NextBtn->setEnabled(true);
		if(iv->rootIndex().row() == 0)
			BackBtn->setEnabled(false);
	}
}

void MainWindowImpl::on_FinishActn_triggered(){
	m->save(fileName + ".res");
}
